package com.mao.action.warehouse;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.mao.action.BaseAction;
import com.mao.model.warehouse.WareHouse;
import com.mao.model.warehouse.WareHouseClass;
import com.mao.service.impl.warehouse.WareHouseCondition;
import com.mao.service.warehouse.IWareHouseClassService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;

public class WareHouseAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private WareHouse w;
	
	private WareHouseCondition wareHouseCondition;

	private IWareHouseService wareHouseService;
	
	private IWareHouseClassService<WareHouseClass> wareHouseClassService;

	private Map<String, Object> result;

	private String wareClassId = "0";

	private Integer page = 0;

	private Integer rows = 0;
	
	private String wareName;

	@Override
	public String execute() {
		return SUCCESS;
	}

	public String jsonSaveWareHouse() {
		w.setStore(SessionHelper.getSessionUser().getStore());
		wareHouseService.saveWareHouse(w);
		return SUCCESS;
	}

	public String jsonGetWarsByWarClass() {
		long storeid = SessionHelper.getSessionUser().getStore().getId();
		if(wareHouseCondition == null)
			wareHouseCondition = new WareHouseCondition();
		if(wareClassId != "0"){
			WareHouseClass wareClass = wareHouseClassService.getWareHouseClassById(new Long(wareClassId));
			wareHouseCondition.setTreePath(wareClass.getTreePath());
		}
		wareHouseCondition.setName(wareName);
		result = new HashMap<String, Object>();
		result.put("total" , wareHouseService.getWarsByWarClassgCount(wareHouseCondition,Long.valueOf(wareClassId),storeid));
		result.put("rows", wareHouseService.getWarsByWarClass(wareHouseCondition,Long.valueOf(wareClassId),storeid,page,rows));
		return SUCCESS;
	}

	public String jsonUpdateWareHouse() {
		WareHouse updateWare = wareHouseService.getWareHouseById(w.getId());
		WareHouseClass wc = null;
		if (w.getWc().getId() != null) {
			wc = wareHouseClassService.getWareHouseClassById(w.getWc().getId());
			updateWare.setWc(wc);
		}
		updateWare.setName(w.getName());
		updateWare.setNo(w.getNo());
		updateWare.setNote(w.getNote());
		wareHouseService.updateWareHouse(updateWare);
		return SUCCESS;
	}
	
	public String delWH(){
		WareHouse updateWare = wareHouseService.getWareHouseById(w.getId());
		updateWare.setStatus(WareHouse.STATUS_DELETED);
		wareHouseService.updateWareHouse(updateWare);
		return SUCCESS;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public WareHouse getW() {
		return w;
	}

	public void setW(WareHouse w) {
		this.w = w;
	}

	public IWareHouseService<WareHouse> getWareHouseService() {
		return wareHouseService;
	}

	public void setWareHouseService(IWareHouseService<WareHouse> wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public String getWareClassId() {
		return wareClassId;
	}

	public void setWareClassId(String wareClassId) {
		this.wareClassId = wareClassId;
	}

	public String getWareName() {
		return wareName;
	}

	public void setWareName(String wareName) {
		this.wareName = wareName;
	}

	public IWareHouseClassService<WareHouseClass> getWareHouseClassService() {
		return wareHouseClassService;
	}
	@Autowired
	public void setWareHouseClassService(
			IWareHouseClassService<WareHouseClass> wareHouseClassService) {
		this.wareHouseClassService = wareHouseClassService;
	}

	public WareHouseCondition getWareHouseCondition() {
		return wareHouseCondition;
	}

	public void setWareHouseCondition(WareHouseCondition wareHouseCondition) {
		this.wareHouseCondition = wareHouseCondition;
	}
	
	
	
}
